import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

interface Response {
  code: number;
  message: string;
  data: unknown;
  timestamp: string;
}

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<Response> {
    return next.handle().pipe(
      map((data: unknown) => {
        return {
          code: 0, // 约定：0 表示成功
          message: 'success',
          data,
          timestamp: new Date().toLocaleString(),
        };
      }),
    );
  }
}
